package com.wondertek.mam.tag.config;

import com.google.common.base.Charsets;
import com.wondertek.mam.tag.tagrep.TagRep;
import com.wondertek.mam.tag.tagrep.impl.TagSqlRep;
import com.wondertek.mam.tag.repository.TagRepository;
import com.wondertek.mam.tag.wordSource.WordSource;
import com.wondertek.mam.tag.wordSource.impl.BaiduHotTodayWordSource;
import com.wondertek.mam.tag.wordSource.impl.SinaHotTodayWordSource;
import org.apache.commons.io.IOUtils;
import org.apdplat.word.segmentation.Segmentation;
import org.apdplat.word.segmentation.SegmentationAlgorithm;
import org.apdplat.word.segmentation.SegmentationFactory;
import org.apdplat.word.util.WordConfTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 标签配置
 * Created by wangdongxu on 2017-09-05 下午 14:19:45.
 */
@Configuration
@EnableJpaRepositories("com.wondertek.mam.tag.repository")
public class TagBeanApplicationConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);

    @Autowired
    private ApplicationProperties applicationProperties;
    @Bean
    public Segmentation initWord(TagRepository tagRepository){

        File thesaurusFile = new File(applicationProperties.getThesaurusFile());
        thesaurusFile.deleteOnExit();
        mkDir(thesaurusFile.getParentFile());
        try {
            FileOutputStream fos = new FileOutputStream(thesaurusFile);
            tagRepository.findAll().parallelStream().forEach(tagDB -> {
                try {
                    IOUtils.write(tagDB.getTag() + "\r", fos, Charsets.UTF_8);
                } catch (IOException e) {
                    LOGGER.error("写入词库文件时出错(tag:" + tagDB.getTag() + ").");
                    e.printStackTrace();
                }
            });
        } catch(FileNotFoundException e){
            LOGGER.error("创建词库文件时出错");
            e.printStackTrace();
        }
        WordConfTools.set("dic.path", applicationProperties.getThesaurusFile());
        Segmentation segmentation = SegmentationFactory.getSegmentation(SegmentationAlgorithm.MaxNgramScore);
        segmentation.seg("内存初始化");
        return segmentation;
    }
    @Bean
    public TagRep tagRep(TagRepository tagRepository){
        return new TagSqlRep(tagRepository);
    }
    @Bean
    public WordSource BaiDuWordSource(TagRep tagRep){
        return new BaiduHotTodayWordSource(tagRep,Constant.BAIDU_HOTWORD_IDS,Constant.BAIDU_HOT_THRESHOLD);
    }
    @Bean
    public WordSource SinaWordSource(TagRep tagRep){
        return new SinaHotTodayWordSource(tagRep, Constant.SINA_HOTWORD_URLAFTER);
    }
    private static void mkDir(File file) {
        if (file.getParentFile().exists()) {
            file.mkdir();
        } else {
            mkDir(file.getParentFile());
            file.mkdir();
        }
    }
}
